package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer findByPic(String pic) {
        Integer count = setMealDao.findByPic(pic);
        return count;
    }

    @Override
    public List<Setmeal> getSetmeal() {
        //1.取出套餐所有id
        List<Integer> setmealIds = setMealDao.findSetmealIds();
        //2.遍历查询出的所有ids，并且根据id统计关系表中是否有关联的检查组
        List<Setmeal> setmeals = new ArrayList<>();
        for (Integer setmealId : setmealIds) {
            Integer count = setMealDao.countSetmealId(setmealId);
            if (count != 0) {
                Setmeal setmeal=setMealDao.findById(setmealId);
                setmeals.add(setmeal);
            }
        }
        return setmeals;
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    @Cacheable
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setMealDao.findById(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        ArrayList<CheckItem> checkItems = new ArrayList<>();
        for (Integer checkgroupId :setMealDao.findSetmealWithCheckGroup(id)) {
            CheckGroup checkGroup = checkGroupDao.selectById(checkgroupId);
            checkGroups.add(checkGroup);
            for (Integer checkItemsId : setMealDao.findCheckgroupWithCheckitem(checkGroup.getId())) {
                CheckItem checkItem= checkItemDao.findById(checkItemsId);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal  ;
    }

}
